$(function(){
	readystu();
	$("#submit-button").click(function(){
		 var stuAge = $("#stuAge").val();
	     var stuQQ = $("#stuQQ").val();
	     var stuPhone = $("#stuPhone").val();
	     var stuClassid = ($("#classID-select").val());
	     var stuaddress = $("#stuAddress").val();
	     var stucode = $("#stuCode").val();
	        $.ajax({
	            url: "/blackhouse/studentinfo",
	            type: "post",
	            data: { "code": 2,"stucode":stucode, "stuAge": stuAge, "stuQQ": stuQQ, "stuPhone": stuPhone, "stuclassid": stuClassid, "stuAddress": stuaddress },
	            dataType: "json",
	            success: function(e) {
	                if (e) {
	                    alert('修改成功');
	                } else {
	                    alert('修改失败');
	                }
	            }
	        })
	})
})
function readystu(){
	$.ajax({
        url: "/blackhouse/studentinfo",
        type: "post",
        data: { "code": 4 },
        dataType: "json",
        success: function(e) {
        	var html=""
        	for(var i = 0;i<e.length;i++){
        		html+="<tr>";
        		html+="<td>"+e[i].stuCode+"</td>";
        		html+="<td>"+e[i].stuName+"</td>";
        		html+="<td>"+e[i].stuSex+"</td>";
        		html+="<td>"+e[i].stuAge+"</td>";
        		html+="<td>"+e[i].stuQQ+"</td>";
        		html+="<td>"+e[i].stuPhone+"</td>";
        		html+="<td>"+e[i].stuclassid+"</td>";
        		html+="<td><button class='btn btn-primary btn-lg' data-toggle='modal' data-target='#myModal' style='font-size: 12px; width: 20px;text-indent: -12px; height: 10px; line-height: 3px!important;'  onclick='changestuinfo(this)' tag='"+e[i].stuCode+"'>" +
        				"修改</button>&nbsp;&nbsp;" +
        				"<button class='btn btn-primary btn-lg' style='font-size: 12px; width: 20px;text-indent: -12px; height: 10px; line-height: 3px!important; tag='"+e[i].stuCode+"' onclick='delstuinfo(this)'>删除</button></td>";
        		html+="</tr>";
        	}
        	$("#student-tbody").html(html);
        }
    })
}
function delstuinfo(e){
	var r=confirm("确认删除该生信息吗");
	if (r==true)
	{
		var key=$(e).attr("tag");
		$.ajax({
	        url: "/blackhouse/studentinfo",
	        type: "post",
	        data: { "code": 1,"key":key },
	        dataType: "json",
	        success: function(e) {
	        		if(e){
	        			swal("删除失败", "后台异常", "error");
	        		}else{
	        			swal("删除成功", "该生已被删除", "success");
	        			readystu();
	        		}
	        	}
	        })
	}
	else
	{
		swal("取消成功", "请求取消", "error");
	}
	
}
function changestuinfo(e){
	readyclass("classID-select");
	var key=$(e).attr("tag");
	$.ajax({
        url: "/blackhouse/studentinfo",
        type: "post",
        data: { "code":"10","key":key },
        dataType: "json",
        success: function(e) {
        	$("#stuName").val(e[0].stuName);
            $("#stuAge").val(e[0].stuAge);
            if (e[0].stuSex == "男") {
                $("#sex1").prop("checked", true);
                $("#sex2").prop("checked", false);
            } else {
                $("#sex2").prop("checked", true);
                $("#sex1").prop("checked", false);
            }
            $("#stuQQ").val(e[0].stuQQ);
            $("#stuPhone").val(e[0].stuPhone);
            $("#classID-select option").each(function(){
        		if($(this).text() == e[0].stuclassid){
        			$(this).attr("selected","selected");
        		}
        	});

            $("#stuAddress").val(e[0].stuAddress);
            $("#stuCode").val(e[0].stuCode);
        	}
        })
}
function seekstuinfo(){
	var num = $("#num").val();
	var name = $("#name").val();
	$.ajax({
		  url: "/blackhouse/Check_student",
	        type: "post",
	        data: {"num":num,"name":name },
	        dataType: "json",
	        success: function(e) {
	        	var html=""
	            	for(var i = 0;i<e.length;i++){
	            		html+="<tr>";
	            		html+="<td>"+e[i].stuCode+"</td>";
	            		html+="<td>"+e[i].stuName+"</td>";
	            		html+="<td>"+e[i].stuSex+"</td>";
	            		html+="<td>"+e[i].stuAge+"</td>";
	            		html+="<td>"+e[i].stuQQ+"</td>";
	            		html+="<td>"+e[i].stuPhone+"</td>";
	            		html+="<td>"+e[i].stuclassid+"</td>";
	            		html+="<td><button class='btn btn-primary btn-lg' data-toggle='modal' data-target='#myModal' style='font-size: 12px; width: 20px;text-indent: -12px; height: 10px; line-height: 3px!important;'>" +
        				"修改</button>&nbsp;&nbsp;<button class='btn btn-primary btn-lg' style='font-size: 12px; width: 20px;text-indent: -12px; height: 10px; line-height: 3px!important; onclick='delstuinfo(this)' tag='"+e[i].stuCode+"'>删除</button></td>";
	            		html+="</tr>";
	            	}
	            $("#student-tbody").html(html);
	        }
	})
}

function readyclass(ID){
	$.ajax({
        url: "/blackhouse/seeclass",
        type: "post",
        dataType: "json",
        success: function(e) {
            var html = "";
            for (var i = 0; i < e.length; i++) {
            	html += "<option value='" + e[i].id + "'>" + e[i].className
				+ "</option>"
            }
            $("#"+ID+"").html(html);
        }
    })
}
